#include "stdafx.h"
#include "TimeValueTestSuite.h"

namespace CO { namespace SpiFoundation { namespace RoomFW {

TimeValueTestSuite::TimeValueTestSuite(void)
{
	TEST_ADD(TimeValueTestSuite::ctor)
	TEST_ADD(TimeValueTestSuite::setGet)
	TEST_ADD(TimeValueTestSuite::assignment)
	TEST_ADD(TimeValueTestSuite::equal)
	TEST_ADD(TimeValueTestSuite::notEqual)
	TEST_ADD(TimeValueTestSuite::greater)
	TEST_ADD(TimeValueTestSuite::greaterEqual)
	TEST_ADD(TimeValueTestSuite::less)
	TEST_ADD(TimeValueTestSuite::lessEqual)
	TEST_ADD(TimeValueTestSuite::plus)
	TEST_ADD(TimeValueTestSuite::minus)
	TEST_ADD(TimeValueTestSuite::plusEqual)
	TEST_ADD(TimeValueTestSuite::minusEqual)
}
	
void TimeValueTestSuite::ctor(void)
{
	::srand(::GetTickCount()); // TODO: MOVE TO SETUP.
	int32_t seconds = ::rand();
	int32_t nanoSeconds = ::rand();

	CO::SpiFoundation::RoomFW::TimeValue tv(seconds, nanoSeconds);

	TEST_ASSERT(tv.seconds() == seconds)
	TEST_ASSERT(tv.nanoSeconds() == nanoSeconds)
}

void TimeValueTestSuite::setGet(void)
{
	CO::SpiFoundation::RoomFW::TimeValue tv;
	int32_t seconds = ::rand();
	int32_t nanoSeconds = ::rand();
	tv.set(seconds, nanoSeconds);

	TEST_ASSERT(tv.seconds() == seconds)
	TEST_ASSERT(tv.nanoSeconds() == nanoSeconds)
}

void TimeValueTestSuite::assignment(void)
{
	int32_t seconds = ::rand();
	int32_t nanoSeconds = ::rand();
	CO::SpiFoundation::RoomFW::TimeValue tv(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv2;
	tv2 = tv;

	TEST_ASSERT(tv == tv2)
}

void TimeValueTestSuite::equal(void)
{
	int32_t seconds = ::rand();
	int32_t nanoSeconds = ::rand();
	CO::SpiFoundation::RoomFW::TimeValue tv1(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv2(seconds, nanoSeconds);

	TEST_ASSERT(tv1 == tv2)
}

void TimeValueTestSuite::notEqual(void)
{
	int32_t seconds = ::rand();
	int32_t nanoSeconds = ::rand();
	CO::SpiFoundation::RoomFW::TimeValue tv1(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv2(seconds + 1, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv3(seconds, nanoSeconds + 1);

	TEST_ASSERT(tv1 != tv2)
	TEST_ASSERT(tv1 != tv3)
	TEST_ASSERT(tv2 != tv3)
}

void TimeValueTestSuite::greater(void)
{
	int32_t seconds = ::rand();
	int32_t nanoSeconds = ::rand();
	CO::SpiFoundation::RoomFW::TimeValue tv1(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv2(seconds, nanoSeconds + 1);
	CO::SpiFoundation::RoomFW::TimeValue tv3(seconds + 1, nanoSeconds);

	TEST_ASSERT(tv2 > tv1)
	TEST_ASSERT(tv3 > tv2)
	TEST_ASSERT(tv3 > tv1)
}

void TimeValueTestSuite::greaterEqual(void)
{
	int32_t seconds = ::rand();
	int32_t nanoSeconds = ::rand();
	CO::SpiFoundation::RoomFW::TimeValue tv1(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv2(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv3(seconds, nanoSeconds + 1);
	CO::SpiFoundation::RoomFW::TimeValue tv4(seconds + 1, nanoSeconds);

	TEST_ASSERT(tv2 >= tv1)
	TEST_ASSERT(tv3 >= tv2)
	TEST_ASSERT(tv4 >= tv3)
	TEST_ASSERT(tv4 >= tv2)
}

void TimeValueTestSuite::less(void)
{
	int32_t seconds = ::rand();
	int32_t nanoSeconds = ::rand();
	CO::SpiFoundation::RoomFW::TimeValue tv1(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv2(seconds, nanoSeconds + 1);
	CO::SpiFoundation::RoomFW::TimeValue tv3(seconds + 1, nanoSeconds);

	TEST_ASSERT(tv1 < tv2)
	TEST_ASSERT(tv2 < tv3)
	TEST_ASSERT(tv1 < tv3)
}

void TimeValueTestSuite::lessEqual(void)
{
	int32_t seconds = ::rand();
	int32_t nanoSeconds = ::rand();
	CO::SpiFoundation::RoomFW::TimeValue tv1(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv2(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv3(seconds, nanoSeconds + 1);
	CO::SpiFoundation::RoomFW::TimeValue tv4(seconds + 1, nanoSeconds);

	TEST_ASSERT(tv1 <= tv2)
	TEST_ASSERT(tv2 <= tv3)
	TEST_ASSERT(tv3 <= tv4)
	TEST_ASSERT(tv2 <= tv4)
}

void TimeValueTestSuite::plus(void)
{
	int32_t seconds = 1;
	int32_t nanoSeconds = 400000000;
	CO::SpiFoundation::RoomFW::TimeValue tv1(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv2(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv3(tv1 + tv2);

	TEST_ASSERT(tv3.seconds() == 2)
	TEST_ASSERT(tv3.nanoSeconds() == 800000000)

	CO::SpiFoundation::RoomFW::TimeValue tv4(tv3);
	CO::SpiFoundation::RoomFW::TimeValue tv5(tv3 + tv4);

	TEST_ASSERT(tv5.seconds() == 5)
	TEST_ASSERT(tv5.nanoSeconds() == 600000000)

	CO::SpiFoundation::RoomFW::TimeValue tv6(1, 500000000);
	CO::SpiFoundation::RoomFW::TimeValue tv7 = tv6 + tv6;
	TEST_ASSERT(tv7.seconds() == 3)
	TEST_ASSERT(tv7.nanoSeconds() == 0)
}

void TimeValueTestSuite::minus(void)
{
	int32_t seconds = 1;
	int32_t nanoSeconds = 400000000;
	CO::SpiFoundation::RoomFW::TimeValue tv1(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv2(seconds * 2, nanoSeconds * 2);
	CO::SpiFoundation::RoomFW::TimeValue tv3(tv2 - tv1);

	TEST_ASSERT(tv3 == tv1)

	CO::SpiFoundation::RoomFW::TimeValue tv4(2, 500000000);
	CO::SpiFoundation::RoomFW::TimeValue tv5(1, 800000000);
	CO::SpiFoundation::RoomFW::TimeValue tv6(tv4 - tv5);

	TEST_ASSERT(tv6.seconds() == 0)
	TEST_ASSERT(tv6.nanoSeconds() == 700000000)

	CO::SpiFoundation::RoomFW::TimeValue tv7(1, 500000000);
	CO::SpiFoundation::RoomFW::TimeValue tv8(0, 500000000);
	TEST_ASSERT((tv7 - tv8) == 1);
	TEST_ASSERT((tv7 - tv7) == 0);
}

void TimeValueTestSuite::plusEqual(void)
{
	int32_t seconds = 1;
	int32_t nanoSeconds = 400000000;
	CO::SpiFoundation::RoomFW::TimeValue tv1(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv2(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv3 = tv1 += tv2;

	TEST_ASSERT(tv3.seconds() == 2)
	TEST_ASSERT(tv3.nanoSeconds() == 800000000)
	TEST_ASSERT(tv3 == tv1)

	CO::SpiFoundation::RoomFW::TimeValue tv4(tv3);
	CO::SpiFoundation::RoomFW::TimeValue tv5 = tv3 += tv4;

	TEST_ASSERT(tv5.seconds() == 5)
	TEST_ASSERT(tv5.nanoSeconds() == 600000000)
	TEST_ASSERT(tv5 == tv3)
}

void TimeValueTestSuite::minusEqual(void)
{
	int32_t seconds = 1;
	int32_t nanoSeconds = 400000000;
	CO::SpiFoundation::RoomFW::TimeValue tv1(seconds, nanoSeconds);
	CO::SpiFoundation::RoomFW::TimeValue tv2(seconds * 2, nanoSeconds * 2);
	CO::SpiFoundation::RoomFW::TimeValue tv3 = tv2 -= tv1;

	TEST_ASSERT(tv3 == tv1)
	TEST_ASSERT(tv3 == tv2)

	CO::SpiFoundation::RoomFW::TimeValue tv4(2, 500000000);
	CO::SpiFoundation::RoomFW::TimeValue tv5(1, 800000000);
	CO::SpiFoundation::RoomFW::TimeValue tv6 = tv4 -= tv5;

	TEST_ASSERT(tv6.seconds() == 0)
	TEST_ASSERT(tv6.nanoSeconds() == 700000000)
	TEST_ASSERT(tv6 == tv4)
}

}}}
